{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.00000000e+00  8.50650808e-01]\n",
      " [ 0.00000000e+00  8.50650808e-01]\n",
      " [ 8.15843973e-16  1.37638192e+00]\n",
      " [ 8.15843973e-16  5.25731112e-01]\n",
      " [ 1.41421356e+00 -6.06576787e-16]\n",
      " [ 7.07106781e-01 -2.02192262e-16]\n",
      " [ 1.41421356e+00 -6.06576787e-16]\n",
      " [ 7.07106781e-01 -4.04384525e-16]]\n",
      "[0.38596491 0.27999429]\n",
      "0.6659592065833291\n"
     ]
    }
   ],
   "source": [
    "from sklearn.decomposition import TruncatedSVD\n",
    "\n",
    "\n",
    "data = [[1, 0, 0, 0],\n",
    "[1, 0, 0, 0],\n",
    "[1, 1, 0, 0],\n",
    "[0, 1, 0, 0],\n",
    "[0, 0, 1, 1],\n",
    "[0, 0, 1, 0],\n",
    "[0, 0, 1, 1],\n",
    "[0, 0, 0, 1]]\n",
    "n_components = 2 # 潜在变量的个数\n",
    "model = TruncatedSVD(n_components=n_components)\n",
    "model.fit(data)\n",
    "print(model.transform(data)) # 变换后的数据\n",
    "print(model.explained_variance_ratio_) # 贡献率 \n",
    "print(sum(model.explained_variance_ratio_)) # 累计贡献率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
